<?php

namespace addons\shopro\controller\car;

use addons\shopro\controller\Base;
use app\common\model\car\CarUserCollectModel;

class Usercollect extends Base
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $model;
    protected $carModel;

    public function __construct()
    {
        parent::__construct();
        $this->model    = new CarUserCollectModel();
        $this->carModel = new \app\admin\model\usedcar\Car();
    }

    /**
     * @menu 添加收藏
     * @Author:男人的坏
     * @Date:2024-05-25 14:55
     */
    public function add()
    {
        $car_id = input('ids');
        if (!$car_id) {
            $this->error('缺少参数');
        }
        $carInfo = $this->carModel->where(['id' => $car_id])->find();
        if (!$carInfo) {
            $this->error('车辆信息不存在');
        }
        $arr['uid']    = $this->auth->id;
        $arr['car_id'] = $car_id;

        $count = $this->model->where($arr)->count();
        if ($count) {
            $this->error('已收藏');
        }
        $this->model->create($arr);
        $this->success('ok');
    }

    /**
     * @menu 收藏列表
     * @Author:男人的坏
     * @Date:2024-05-25 15:01
     */
    public function index()
    {
        $data = $this->model->where(['uid' => $this->auth->id])->with(['car'])->paginate();
        $this->success('ok', $data);
    }

    /**
     * @menu:删除收藏
     * @Author:男人的坏
     * @Date:2024-05-25 15:05
     */

    public function del()
    {
        $ids = input('ids');
        if (!$ids) {
            $this->error('缺少参数');
        }
        $type = input('type',1);
        $where['uid'] = $this->auth->id;
        if($type == 1){
            $where['car_id'] = $ids;
        }else{
            $where['id']     = $ids;
        }
        $info = $this->model->where($where)->find();
        if (!$info) {
            $this->error('数据不存在');
        }
        $info->delete();
        $this->success('ok');
    }

}
